package com.gec.hrm.servlet;

import com.gec.hrm.dao.impl.EmployeeDao;
import com.gec.hrm.dao.impl.EmployeeDaoImpl;
import com.gec.hrm.pojo.Employee;
import com.gec.hrm.pojo.R;
import com.gec.hrm.util.FormatStringAsDate;
import com.google.gson.Gson;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Date;
import java.util.List;

@WebServlet(urlPatterns = {"/employeeList.action","/checkCardId.action","/addEmployee.action","/delEmployee.action","/delEmployees.action","/updEmployee.action"})
public class EmployeeServlet extends HttpServlet {
    EmployeeDao employeeDao=new EmployeeDaoImpl();

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String url = request.getRequestURI();
        System.out.println(url);
        String action = url.substring(url.lastIndexOf("/") + 1);
        if(action.equals("employeeList.action")){
            employeeList(request,response);
        }else if(action.equals("checkCardId.action")){
            checkCardId(request,response);
        }else if(action.equals("addEmployee.action")){
            addEmployee(request,response);
        }else if(action.equals("delEmployee.action")){
            delEmployee(request,response);
        }else if(action.equals("delEmployees.action")){
            delEmployees(request,response);
        }else if(action.equals("updEmployee.action")){
            updEmployee(request,response);
        }
    }

    private void updEmployee(HttpServletRequest request, HttpServletResponse response)throws IOException{
        response.setCharacterEncoding("UTF-8");
        PrintWriter out = response.getWriter();
        int id=Integer.parseInt(request.getParameter("id"));
        String name=request.getParameter("name");
        String cardId=request.getParameter("cardId");
        String sex=request.getParameter("sex");
        String jobId=request.getParameter("jobId");
        String education=request.getParameter("education");
        String email=request.getParameter("email");
        String phone=request.getParameter("phone");
        String tel=request.getParameter("tel");
        String party=request.getParameter("party");
        String qqNum=request.getParameter("qqNum");
        String address=request.getParameter("address");
        String postCode=request.getParameter("postCode");
        String birthday=request.getParameter("birthday");
        String race=request.getParameter("race");
        String speciality=request.getParameter("speciality");
        String hobby=request.getParameter("hobby");
        String remark=request.getParameter("remark");
        String deptId=request.getParameter("deptId");
        Employee employee=new Employee(id,name,cardId,address,phone,email,Integer.parseInt(sex),education,
                Integer.parseInt(deptId),Integer.parseInt(jobId),tel,postCode,
                qqNum,party,FormatStringAsDate.formart(birthday),race,speciality,hobby,remark);
        int i = employeeDao.updEmployee(employee);
        if(i>0){
            out.print(1);
        }else {
            out.print(0);
        }
    }

    private void delEmployees(HttpServletRequest request, HttpServletResponse response) throws IOException{
        String ids = request.getParameter("ids");
        var id = ids.split(",");

        PrintWriter out = response.getWriter();
        for (var ix : id) {
            int i = employeeDao.delEmployee(Integer.parseInt(ix));
            if (i < 1) {
                out.print(0);
                return;
            }
        }
        out.print(1);
    }

    private void delEmployee(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String id = request.getParameter("id");
        int i = employeeDao.delEmployee(Integer.parseInt(id));
        PrintWriter out = response.getWriter();
        if (i < 1) {
            out.print(0);
        }else {
            out.print(1);
        }
    }

    private void addEmployee(HttpServletRequest request, HttpServletResponse response) throws IOException {
        response.setCharacterEncoding("UTF-8");
        PrintWriter out = response.getWriter();
        String name=request.getParameter("name");
        String cardId=request.getParameter("cardId");
        String sex=request.getParameter("sex");
        String jobId=request.getParameter("jobId");
        String education=request.getParameter("education");
        String email=request.getParameter("email");
        String phone=request.getParameter("phone");
        String tel=request.getParameter("tel");
        String party=request.getParameter("party");
        String qqNum=request.getParameter("qqNum");
        String address=request.getParameter("address");
        String postCode=request.getParameter("postCode");
        String birthday=request.getParameter("birthday");
        String race=request.getParameter("race");
        String speciality=request.getParameter("speciality");
        String hobby=request.getParameter("hobby");
        String remark=request.getParameter("remark");
        String deptId=request.getParameter("deptId");
        Employee employee=new Employee(name,cardId,address,phone,email,Integer.parseInt(sex),education,
        new Date(),Integer.parseInt(deptId),Integer.parseInt(jobId),postCode,qqNum,party, FormatStringAsDate.formart(birthday),race,0,speciality
        ,hobby,remark,tel);
        int i = employeeDao.addEmployee(employee);
        if(i>0){
            out.print(1);
        }else {
            out.print(0);
        }
    }

    private void checkCardId(HttpServletRequest request, HttpServletResponse response) throws IOException {
        response.setCharacterEncoding("UTF-8");
        PrintWriter out = response.getWriter();
        String cardId = request.getParameter("cardId");
        boolean flag = employeeDao.selectCardId(cardId);
        if(flag){
            out.print(0);
        }else {
            out.print(1);
        }
    }

    private void employeeList(HttpServletRequest request, HttpServletResponse response) throws IOException{
       response.setCharacterEncoding("UTF-8");
       PrintWriter out = response.getWriter();
       String page = request.getParameter("page");
       String limit = request.getParameter("limit");
       String name = request.getParameter("name");
       int sex =request.getParameter("sex")!="" && request.getParameter("sex")!=null ? Integer.parseInt(request.getParameter("sex")):-1;
       String cardId = request.getParameter("cardId");
       int jobId =request.getParameter("jobId")!="" && request.getParameter("jobId")!=null ? Integer.parseInt(request.getParameter("jobId")):0;
       int deptId =request.getParameter("deptId")!="" && request.getParameter("deptId")!=null ? Integer.parseInt(request.getParameter("deptId")):0;
       String phone = request.getParameter("phone");
       Employee employee = new Employee(name,cardId,phone,sex,deptId,jobId);
       List<Employee> employees = employeeDao.employeeList(employee,Integer.parseInt(page),Integer.parseInt(limit));
       R r = new R();
       r.put("msg","查询成功");
       r.put("data",employees);
       r.put("count",employeeDao.count());
       r.put("code",0);
       out.print(new Gson().toJson(r));
   }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}
